//hash change
var is_hashchange = true;
var hash_url = "engine";
$(window).bind( 'hashchange', function(e) {			
	if(!is_hashchange)
	{
		is_hashchange = true;
		return;
	}
	
	var key = $.param.fragment();
	key = key == ""?hash_url:key;
	var func = window[key];
	var obj = func();
	var url = obj.url;
	showLoader();
	var container = getMainContent();
	container.load(url,function(response){
		container.html(response);
		if(typeof(obj.callback) != "undefined"  && typeof(obj.callback != null))
		{
			obj.callback();
		}
		$(".hideOnload").show();
		hideLoader();
	})
	
});

function disableHashchange()
{
	is_hashchange = false;
}

function enableHashchange()
{
	is_hashchange = true;
}

function setHash(hashname,arr)
{
	var localStorage = window.localStorage;	
	localStorage.setItem(hashname,JSON.stringify(arr));
}

function engine()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=engine&tmpl=raw";
	var callback = function(){
		if($("#grid_engine").length > 0)
		{
			var dataSource = $("#grid_engine").data("kendoGrid").dataSource;
			dataSource.read();
		}
		
	}
	obj.callback = callback;
	return obj;
}

function create_engine()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=engine&layout=create&tmpl=raw";
	var callback = function(){
		if($("#tab_create_engine").length > 0)
		{
			initControl("#tab_create_engine");
			initCreateEngine();
		}
		
	}
	obj.callback = callback;
	return obj;
}

function edit_engine()
{
	var localStorage = window.localStorage;	
	var item_arr = localStorage.getItem("edit_engine");
	if(item_arr == null)
	{
		window.location.hash = "engine";
		return;
	}
	var item = JSON.parse(item_arr);
	var id = item.id;
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=engine&layout=create&tmpl=raw&id="+id;
	var callback = function(){
		if($("#tab_create_engine").length > 0)
		{
			initControl("#tab_create_engine");
			initCreateEngine();
		}
	}
	obj.callback = callback;
	return obj;
}

function linhkien()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=linhkien&tmpl=raw";
	var callback = function(){
		if($("#grid_linhkien").length > 0)
		{
			var dataSource = $("#grid_linhkien").data("kendoGrid").dataSource;
			dataSource.read();
		}
	}
	obj.callback = callback;
	return obj;
}

function create_linhkien()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=linhkien&layout=create&tmpl=raw";
	var callback = function(){
		if($("#tab_create_linhkien").length > 0)
		{
			initControl("#tab_create_linhkien");
			initCreateLinhkien();
		}	
		
	}
	obj.callback = callback;
	return obj;
}

function edit_linhkien()
{
	var localStorage = window.localStorage;	
	var item_arr = localStorage.getItem("edit_linhkien");
	if(item_arr == null)
	{
		window.location.hash = "linhkien";
		return;
	}
	var item = JSON.parse(item_arr);
	var id = item.id;
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=linhkien&layout=create&tmpl=raw&id="+id;
	var callback = function(){
		if($("#tab_create_linhkien").length > 0)
		{
			initControl("#tab_create_linhkien");
			initCreateLinhkien();
		}
	}
	obj.callback = callback;
	return obj;
}

